package trunk.gui;

import trunk.control.Control;
import trunk.res.R;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * panel used for input or select the parameters to display the simulations
 * you can select instructions like "MOV", and select the source and destination
 * Created by IntelliJ IDEA.
 * User: yipeipei
 * Date: 12/5/11
 * Time: 4:59 PM
 * To change this template use File | Settings | File Templates.
 */
public class InstructionPanel extends JPanel implements ActionListener {
    private JLabel label_instructionType = new JLabel("指令类型");
    private JLabel label_instruction = new JLabel("指令");
    private JLabel label_sourceOperand = new JLabel("源操作数");
    private JLabel label_targetOperand = new JLabel("目的操作数");

    private JComboBox jComboBox_instructionType;
    private JComboBox jComboBox_instruction;
    private JComboBox jComboBox_sourceOperand;
    private JComboBox jComboBox_targetOperand;

    public InstructionPanel() {
        super(true);    //null for do not use a layout, true for enable double buffer
        this.setBounds(
                R.attribute.SIM_PANEL_WIDTH,
                0,
                R.attribute.INSTRUCTION_PANEL_WIDTH,
                R.attribute.INSTRUCTION_PANEL_HEIGHT);    //set a fixed location in its container
        this.setBorder(R.attribute.BORDER);
        this.setLayout(new GridLayout(4, 2, 0, 6));

        initialize();   //add components

        this.setVisible(true);
    }

    /**
     * initializeAuxiliary the components
     */
    public void initialize() {
        this.add(label_instructionType);
        this.add(this.jComboBox_instructionType = initComboBox(R.string.INSTRUCTION_TYPE, 0, this));
        Control.getInstant().setInstructionType(0);   //parameter transfer

        this.add(label_instruction);
        this.add(this.jComboBox_instruction = initComboBox(R.string.INSTRUCTION_TRANSFER, 0, this));
        Control.getInstant().setInstruction(0);

        this.add(label_sourceOperand);
        this.add(this.jComboBox_sourceOperand = initComboBox(R.string.SOURCE_OPERAND_ADDRESS_MODE, 0, this));
        Control.getInstant().setSourceOperandMod(0);

        this.add(label_targetOperand);
        this.add(this.jComboBox_targetOperand = initComboBox(R.string.TARGET_OPERAND_ADDRESS_MODE, 0, this));
        Control.getInstant().setTargetOperandMod(0);

    }

    //initialize a jComboBox
    private JComboBox initComboBox(String[] items, int selectedIndex, ActionListener actionListener) {
        JComboBox jComboBox = new JComboBox(items);
        jComboBox.setSelectedIndex(selectedIndex);
        jComboBox.addActionListener(actionListener);

        return jComboBox;
    }

    protected void enableSelect(){
        this.jComboBox_instructionType.setEnabled(true);
        this.jComboBox_instruction.setEnabled(true);
        this.jComboBox_sourceOperand.setEnabled(true);
        this.jComboBox_targetOperand.setEnabled(true);
    }

    protected void disableSelect(){
        this.jComboBox_instructionType.setEnabled(false);
        this.jComboBox_instruction.setEnabled(false);
        this.jComboBox_sourceOperand.setEnabled(false);
        this.jComboBox_targetOperand.setEnabled(false);
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == this.jComboBox_instructionType) {
            int selectedIndex = this.jComboBox_instructionType.getSelectedIndex();

            Control.getInstant().setInstructionType(selectedIndex);
            //enable source operand mode
            if (selectedIndex == 1) {     //1 for single operand
                this.jComboBox_sourceOperand.setEnabled(false);
                Control.getInstant().setSourceOperandMod(-1); //-1 for invalid
            } else {
                this.jComboBox_sourceOperand.setEnabled(true);
            }
            //refresh jComboBox_instruction
            this.jComboBox_instruction.removeAllItems();
            for (String string : R.string.INSTRUCTION[selectedIndex]) {
                this.jComboBox_instruction.addItem(string);
            }
        } else if (e.getSource() == this.jComboBox_instruction) {
            int selectedIndex = this.jComboBox_instruction.getSelectedIndex();

            Control.getInstant().setInstruction(selectedIndex);
        } else if (e.getSource() == this.jComboBox_sourceOperand) {
            int selectedIndex = this.jComboBox_sourceOperand.getSelectedIndex();

            Control.getInstant().setSourceOperandMod(selectedIndex);
        } else if (e.getSource() == this.jComboBox_targetOperand) {
            int selectedIndex = this.jComboBox_targetOperand.getSelectedIndex();

            Control.getInstant().setTargetOperandMod(selectedIndex);
        }
    }
}
